// 角色管理模块接口
import request from "@/utils/request";
import type { RoleResponseData, RoleData, MenuResponseData } from "./type";

enum API {
  // 获取角色列表
  ALLROLE_URL = "/admin/acl/role/",
  //  新增角色
  ADDROLE_URL = "/admin/acl/role/save",
  // 修改角色
  UPDATEROLE_URL = "/admin/acl/role/update",
  //   根据角色获取菜单
  ALLPERMISSTION = "/admin/acl/permission/toAssign/",
  // 给角色分配权限
  SETPERMISSTION_URL = "/admin/acl/permission/doAssign",
  //   删除已有的职位
  REMOVEROLE_URL = "/admin/acl/role/remove/",
}

// 获取全部角色列表
export const reqAllRoleList = (page: number, limit: number, roleName: string) =>
  request.get<any, RoleResponseData>(
    API.ALLROLE_URL + `${page}/${limit}/?roleName=${roleName}`
  );

// 新增角色或者修改角色
export const reqAddOrUpdateRole = (data: RoleData) => {
  if (data.id) {
    // 更新
    return request.put<any, any>(API.UPDATEROLE_URL, data);
  } else {
    // 新增
    return request.post<any, any>(API.ADDROLE_URL, data);
  }
};

// 根据角色获取菜单
export const reqAllMenuList = (roleId: number) =>
  request.get<any, MenuResponseData>(API.ALLPERMISSTION + roleId);

// 给角色分配权限
export const reqSetPermisstion = (roleId: number, permissionId: number[]) =>
  request.post<any, any>(
    API.SETPERMISSTION_URL + `?roleId=${roleId}&permissionId=${permissionId}`
  );

//   删除已有的职位
export const reqRemoveRole = (roleId: number) =>
  request.delete<any, any>(API.REMOVEROLE_URL + roleId);
